/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 19.8.2012
 *
 *************************************************/
package fightplan.persistence;

import static fightplan.persistence.ImportHelper.getBoolean;
import static fightplan.persistence.ImportHelper.getString;

import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import fightplan.business.Category;
import fightplan.business.Dojo;
import fightplan.business.Fighter;
import fightplan.business.Gender;
import fightplan.business.Tournament;

/**
 * Import für das Herbst 2012 eingeführte Ippon-Shobu Turnier.
 * Erstellt nach Vorschlag von Christian
 * 
 * @author Bruno
 */
public class ExcelIpponShobuImport {

	static void imprtFightersAndTeams(Tournament tournament, File file) throws Exception {
		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
		HSSFWorkbook wb = new HSSFWorkbook(fs);

		HSSFSheet sheet = wb.getSheetAt(0);

		// ////////////////////////////////////////////////
		// if (!checkSheet(sheet, (short)0, (short)36, "Format 080315")) throw
		// new IllegalArgumentException("Kein gültiges Excelsheet: " +  file.getName());

		// Dojo
		String dojoName = getString(sheet.getRow(3), 2);
		Dojo dojo = tournament.getOrCreateDojo(dojoName);

		// read Fighters
		readFighters(tournament, dojo, sheet);
	}

	private static void readFighters(Tournament tournament, Dojo dojo, HSSFSheet sheet) {
		for (int i = 13; i < 169; i++) {
			HSSFRow row = sheet.getRow(i);
			if (row == null)
				continue;
			String fighterName = getString(row, 1);
			if (fighterName == null || fighterName.length() == 0)
				continue;

			fighterName = ImportHelper.normalizeName(fighterName);

			Fighter fighter = new Fighter();
			fighter.setName(fighterName);
			fighter.setDojo(dojo);

			int c = 4; // column

			try {
				fighter.setYearFormatted(getString(row, c++));
			} catch (IllegalArgumentException iae) {
				// silent
			}

			// ignore kyu
			c++;

			// U 16
			readKataColumn(tournament, fighter, row, c++, true, Gender.D);
			readKataColumn(tournament, fighter, row, c++, true, Gender.H);

			// U 18
			readKataColumn(tournament, fighter, row, c++, true, Gender.D);
			readKataColumn(tournament, fighter, row, c++, true, Gender.H);

			// 18+
			readKataColumn(tournament, fighter, row, c++, true, Gender.D);
			readKataColumn(tournament, fighter, row, c++, true, Gender.H);

			// U 16
			readIpponColumn(tournament, fighter, row, c++, Gender.D);
			readIpponColumn(tournament, fighter, row, c++, Gender.H);

			// U 18
			readIpponColumn(tournament, fighter, row, c++, Gender.D);
			readIpponColumn(tournament, fighter, row, c++, Gender.H);

			// 18+
			readIpponColumn(tournament, fighter, row, c++, Gender.D);
			readIpponColumn(tournament, fighter, row, c++, Gender.H);
			
			tournament.getFighters().add(fighter);
		}
	}

	private static void readKataColumn(Tournament tournament, Fighter fighter, HSSFRow row, int column, boolean kata,
			Gender gender) {
		if (getBoolean(row, column)) {
			fighter.setGender(gender);
			Category category = tournament.findCategory(fighter, kata, false); // Kata,
																				// elite
																				// argument
																				// is
																				// ignored
			if (category != null) {
				category.getFighters().add(fighter);
			} else {
				System.out.println("Probleme mit Kata: " + fighter.getName());
			}
		}
	}

	private static void readIpponColumn(Tournament tournament, Fighter fighter, HSSFRow row, int column, Gender gender) {
		if (getBoolean(row, column)) {
			if (gender != null) {
				fighter.setGender(gender);
			}
			Category category = tournament.findKumiteCategory(fighter, 0, Integer.MAX_VALUE);
			if (category != null) {
				category.getFighters().add(fighter);
			} else {
				System.out.println("Probleme mit: " + fighter.getName() + " column: " + column);
			}
		}
	}

}
